<?php

namespace app\api\controller\web;

use app\admin\model\apply\Video;
use app\admin\model\memberpoint\Fellow;
use app\admin\model\memberpoint\Integral;
use app\admin\model\nursery\Companyattestation;
use app\admin\model\nursery\NurserySell;
use app\admin\model\nursery\NurseryWantbuyOffer;
use app\admin\model\nursery\Specs;
use app\admin\model\nursery\Wantbuy;
use app\api\controller\server\Server;
use app\api\controller\User;
use app\common\controller\Api;
use app\common\helpers\ImageUtil;
use app\common\model\Attachment;
use app\common\model\ScoreLog;
use think\Db;
use think\Log;
use think\Model;
use think\Validate;

/**
 *  个人中心
 */
class Center extends Api
{
    protected $noNeedLogin = ['teachingVideo','systemConfig'];
    protected $noNeedRight = '*';
    protected $model ='';
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 教学视频
     */
    public function teachingVideo()
    {
        $info = (new Video())->order('id desc')->find();
        if($info && $info['url']){
            $info['url'] = ImageUtil::handle($info['url']);
        }
        $this->success("成功", $info);
    }


    /**
     * 我的求购列表
     */
    public function myWantBuyList()
    {
        $user = $this->auth->getUser();
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量
        $model = (new Wantbuy())
            ->field('id,name,imgs,specs_config,address,create_time,num,lvl,introduction,status')
            ->where(['user_id'=>$user['id'],'is_del'=>0])
            ->order('create_time desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);
        $total = $model->total();
        $list =  $model->items();
        if($list){
            $specsList = (new Specs())->select();
            $nurseryLevel =  \app\common\common\Common::$nurseryLevel;
            foreach ($list as &$v){
                $v['img'] = '';
                $v['img'] = '';
                $v['imgs'] =ImageUtil::handleMore($v['imgs']);
                if(count($v['imgs']) > 0){
                    $v['img'] = $v['imgs'][0];
                }

                $v['lvl_name'] = isset($nurseryLevel[$v['lvl']]) ?  $nurseryLevel[$v['lvl']] : '';
                $v['specs_config'] = Index::specsConfig($v['specs_config'],$specsList);
            }
        }
        $result = array("total" =>$total , "list" => $list);
        $this->success("请求成功", $result);
    }


    /**
     * 商家报价列表
     */
    public function businessQuotelist()
    {
        $wantbuy_id = $this->request->param('wantbuy_id');
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量

        $model =  (new NurseryWantbuyOffer())
            ->where(['wantbuy_id'=>$wantbuy_id,'disable'=>1])
            ->order('create_time desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);
        $total = $model->total();
        $list =  $model->items();
        if($list){
            foreach ($list as &$v){
                $v['img'] = '';
                $v['imgs'] =ImageUtil::handleMore($v['imgs']);
                if(count($v['imgs']) > 0){
                    $v['img'] = $v['imgs'][0];
                }
            }
        }
        $result = array("total" =>$total , "list" => $list);
        $this->success("请求成功", $result);
    }



    /**
     * 商家报价详情
     */
    public function businessQuoteDetails()
    {
        $wantbuy_offer_id = $this->request->param('wantbuy_offer_id');
        $info =  (new NurseryWantbuyOffer())
            ->where(['id'=>$wantbuy_offer_id])
            ->find();
        if($info){
            $info['img'] = '';
            $info['imgs'] =ImageUtil::handleMore($info['imgs']);
            if(count($info['imgs']) > 0){
                $info['img'] = $info['imgs'][0];
            }
            $this->success("请求成功", $info);
        }else{
            $this->error("数据不存在");
        }
    }

    /**
     * 我的报价列表
     */
    public function myOfferList()
    {

        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量
        $user = $this->auth->getUser();
        $model = (new Wantbuy())->alias('w')
            ->field('w.id,w.name,w.imgs,w.specs_config,w.address,w.create_time,w.num,w.lvl,w.introduction,wo.price')
            ->join('nursery_wantbuy_offer wo','w.id = wo.wantbuy_id','inner')
            ->where(['wo.user_id'=>$user['id'],'status'=>1]) //这和测试沟通过  不展示下架的
            ->order('wo.create_time desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);
        $total = $model->total();
        $list =  $model->items();
        if($list){
            $specsList = (new Specs())->select();
            foreach ($list as &$v){
                $v['img'] = '';
                $v['imgs'] =ImageUtil::handleMore($v['imgs']);
                if(count($v['imgs']) > 0){
                    $v['img'] = $v['imgs'][0];
                }
                $v['specs_config'] = Index::specsConfig($v['specs_config'],$specsList);
            }
        }
        $result = array("total" =>$total , "list" => $list);
        $this->success("请求成功", $result);
    }

    /**
     * 我的供应列表
     */
    public function mySupplyList()
    {
        $user = $this->auth->getUser();
        $search_name = $this->request->param('search_name');
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量
        if($search_name){
            $where['ns.name'] = ['like','%'.$search_name.'%'];
        }
        $where['user_id'] = $user['id'];
        $model =  (new NurserySell())->alias('ns')
            ->field('ns.id,user_id,ns.name,ns.imgs,ns.introduction,city_id,ns.specs_config,ns.lvl,ns.browse_num,ns.call_num,ns.price,ns.address,ns.create_time,refresh_time,u.nickname,u.level')
            ->join('user u','ns.user_id = u.id','left')
            ->where($where)
            ->order('ns.create_time desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);

        $total = $model->total();
        $list =  $model->items();
        if($list){
            $specsList = (new Specs())->select();
            $nurseryLevel =  \app\common\common\Common::$nurseryLevel;
            $cityList = Server::getInstance()->getAreaList(['level'=>2]); //(['level'=>2]);
            $cityList = array_column($cityList,'name','id');
            foreach ($list as &$v){
                $v['img'] = '';
                $v['imgs'] =ImageUtil::handleMore($v['imgs']);
                if(count($v['imgs']) > 0){
                    $v['img'] = $v['imgs'][0];
                }
                $v['lvl_name'] = isset($nurseryLevel[$v['lvl']]) ?  $nurseryLevel[$v['lvl']] : '';
                $v['specs_config'] = Index::specsConfig($v['specs_config'],$specsList);
                $v['city_name'] = $cityList[$v['city_id']] ??'';
            }
        }
        $result = array("total" =>$total , "list" => $list);
        $this->success("请求成功", $result);
    }


    /**
     * 我的报价列表 -我的报价
     */
    public function myOfferMyOfferList()
    {
        $user = $this->auth->getUser();
        $wantbuy_id = $this->request->param('wantbuy_id');
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量

        $model =  (new NurseryWantbuyOffer())
            ->where(['wantbuy_id'=>$wantbuy_id,'user_id'=>$user['id']])
            ->order('create_time desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);
        $total = $model->total();
        $list =  $model->items();
        if($list){
            foreach ($list as &$v){
                $v['img'] = '';
                $v['imgs'] =ImageUtil::handleMore($v['imgs']);
                if(count($v['imgs']) > 0){
                    $v['img'] = $v['imgs'][0];
                }
            }
        }
        $result = array("total" =>$total , "list" => $list);
        $this->success("请求成功", $result);
    }


    /**
     * 我积分列表
     */
    public function myIntegralList()
    {
        $user = $this->auth->getUser();
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量

        $model =  (new ScoreLog())
            ->field('id,score,memo,createtime')
            ->where(['user_id'=>$user['id']])
            ->order('createtime desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);

        $total = $model->total();
        $list =  $model->items();
        if($list){
            foreach ($list as $v){
                $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            }
        }
        $result = array("total" =>$total , "list" => $list , "score"=>$user['score']);
        $this->success("请求成功", $result);
    }


    /**
     * 我的会员
     */
    public function myMember()
    {
        $user = $this->auth->getUser();

        if($user['member_expire_time'] <= date('Y-m-d H:i:s')){
            \app\admin\model\User::update(['level'=>0,'member_expire_time'=>null],['id'=>$user['id']]);
            $user = $this->auth->getUser();
        }
        $member_info = [
            'lvl'=> $user['level'] ,
//            'last_lvl'=> $last_lvl ,
//            'start_time'=> isset($info['start_time']) ? date('Y-m-d' ,strtotime($info['start_time'])) : '',
            'end_time'=> $user['member_expire_time'] ? date('Y-m-d' ,strtotime($user['member_expire_time'])) : '',
        ];
        $this->success("请求成功", $member_info);

//        $info = Db::name('user_buy_log')->where(['user_id'=>$user['id']])->order('id desc')->find();
//        $lvl = 0;
//        $last_lvl = 0;
//        if($info){
//             if(strtotime($info['end_time']) >= time()){
//                 $lvl = $info['f_id'];
//             }else{
//                 $last_lvl =$info['f_id'];
//             }
//        }
//        if($user['level'] != $lvl){ //
//            \app\admin\model\User::update(['level'=>$lvl],['id'=>$user['id']]);
//        }
//        $member_info = [
//            'lvl'=> $lvl ,
//            'last_lvl'=> $last_lvl ,
//            'start_time'=> isset($info['start_time']) ? date('Y-m-d' ,strtotime($info['start_time'])) : '',
//            'end_time'=> isset($info['end_time']) ? date('Y-m-d' ,strtotime($info['end_time'])) : '',
//        ];
//        $this->success("请求成功", $member_info);
    }


    /**
     * 获取公司认证信息
     */
    public function myCompanyAuth()
    {
        $user = $this->auth->getUser();
        $info = (new Companyattestation())->field('company_name,gacade_img,business_img,phone,area,introduction')->where(['user_id'=>$user['id']])->find();
        if($info){
            $info['gacade_img_url'] = ImageUtil::handle($info['gacade_img']);
            $info['business_img_url'] = ImageUtil::handle($info['business_img']);
        }
        $this->success("请求成功", $info);
    }





    /**
     * 获取系统配置
     */
    public function systemConfig(){
        $member_list = (new Fellow())->select();
        $Integral_list = (new Integral())->select();
        $Confg = \app\admin\model\apply\Confg::get(1);
        $this->success("请求成功", ['member_list'=>$member_list,'integral_info'=>$Integral_list,'sys_config'=>$Confg]);
    }






}
